package com.likai.authority.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.likai.authority.dao.RoleMapper;
import com.likai.authority.pojo.Role;
import com.likai.authority.service.IRoleService;
import com.likai.authority.util.IdUtil;

@Service("roleService")
public class RoleServiceImpl implements IRoleService {

	@Autowired
	private RoleMapper roleMapper ;
	
	@Transactional(readOnly=true)
	public Role findByRoleId(String roleId) {
		return roleMapper.selectByPrimaryKey(roleId);
	}

	@Transactional(readOnly=true)
	public List<Role> findList(Role po) {
		return roleMapper.selectByExample(po);
	}

	@Transactional
	public int removeByIds(String[] ids) {
		boolean flag = false ;
		for (String id : ids) {
			if(roleMapper.deleteByPrimaryKey(id) == 1) {
				flag = true ;
			}
		}
		if(flag) {
			return 1 ;
		} else {
			return 0 ;
		}
	}

	public int updateRole(Role po) {
		boolean flag = false ;
		String roleid = po.getRoleid() ;
		if(roleid == null || "".equals(roleid)) {
			po.setRoleid(IdUtil.uuid());
			if(roleMapper.insertSelective(po) == 1) {
				flag = true ;
			}
		} else {
			if(roleMapper.updateByPrimaryKeySelective(po) == 1) {
				flag = true ;
			}
		}
		if(flag) {
			return 1 ;
		} else {
			return 0 ;
		}
	}

	public Role findByRolename(String rolename) {
		return roleMapper.selectByRolename(rolename);
	} 

}
